package org.jeecg.modules.modelBase.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.dataSet.entity.DataSet;
import org.jeecg.modules.dataSet.service.IDataSetService;
import org.jeecg.modules.modelBase.entity.ModelBase;
import org.jeecg.modules.modelBase.entity.ModelBaseData;
import org.jeecg.modules.modelBase.service.IModelBaseDataService;
import org.jeecg.modules.modelBase.service.IModelBaseService;
import org.jeecg.modules.modelBase.vo.ModelParam;
import org.jeecg.modules.modelBase.vo.TrainLogParam;
import org.jeecg.modules.userFormula.entity.UserFormulaDetail;
import org.jeecg.modules.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 模型模板
 */
@Api(tags = "模型模板")
@RestController
@RequestMapping("/modelBase")
@Slf4j
public class TemplateController extends JeecgController<ModelBase, IModelBaseService> {
    @Value("${zf-mxxl-service.url}")
    private String zfMxxlServiceUrl;

    @ApiOperation(value = "模型模板-添加", notes = "模型模板-添加")
    @PostMapping(value = "/template/add")
    public Result<Object> templateAdd(@RequestBody HashMap map, HttpServletRequest req) {
        String url = zfMxxlServiceUrl + "/model/add_template";
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("name", map.get("name"));
        paramsMap.put("data_source", map.get("dataSource"));
        paramsMap.put("iteration", map.get("iteration"));
        paramsMap.put("seq_length", map.get("seqLength"));
        paramsMap.put("train_percent", map.get("trainPercent"));
        paramsMap.put("selected_columns", map.get("selectedColumns"));
        paramsMap.put("predict_column", map.get("predictColumn"));
        paramsMap.put("remark", map.get("remark"));
        JSONObject object = HttpClientUtil.sendPost(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            return Result.OK(object.get("result"));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("模型模板添加失败");
            }
        }
    }

    @ApiOperation(value = "模型模板-删除", notes = "模型模板-删除")
    @DeleteMapping(value = "/template/delete")
    public Result<Object> templateDelete(@RequestParam(name = "id") String templateId, HttpServletRequest req) {
        String url = zfMxxlServiceUrl + "/model/delete_template";
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("ids", templateId);
        JSONObject object = HttpClientUtil.sendPost(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            return Result.OK(object.get("result"));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("模型模板删除失败");
            }
        }
    }

    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "模型模板-分页列表查询")
    @ApiOperation(value = "模型模板-分页列表查询", notes = "模型模板-分页列表查询")
    @GetMapping(value = "/template/list")
    public Result<IPage<Object>> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {
        IPage<Object> pageList = new Page<>();
        String url = zfMxxlServiceUrl + "/model/get_template";
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("page", pageNo.toString());
        paramsMap.put("size", pageSize.toString());
        JSONObject object = HttpClientUtil.sendGet(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            JSONObject result = (JSONObject) object.get("data");
            pageList.setRecords((List<Object>) result.get("rows"));
            pageList.setTotal(Long.parseLong(result.get("total").toString()));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("获取模型模板失败");
            }
        }

        return Result.OK(pageList);
    }

    @ApiOperation(value = "模型列表-分页列表查询", notes = "模型列表-分页列表查询")
    @GetMapping(value = "/model/list")
    public Result<IPage<Object>> queryModelPageList(ModelParam modelParam, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        IPage<Object> pageList = new Page<>();
        String url = zfMxxlServiceUrl + "/model/list";
        if (StringUtils.isNotEmpty(modelParam.getSlotId())) {
            url += "?slot_id=" + modelParam.getSlotId();
        }
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("page", pageNo.toString());
        paramsMap.put("size", pageSize.toString());
        paramsMap.put("name", modelParam.getName());
        JSONObject object = HttpClientUtil.sendGet(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            pageList.setRecords((List<Object>) object.get("rows"));
            pageList.setTotal(Long.parseLong(object.get("total").toString()));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("获取模型列表失败");
            }
        }

        return Result.OK(pageList);
    }

    @ApiOperation(value = "模型预测", notes = "模型预测")
    @PostMapping(value = "/model/predict")
    public Result<Object> predict(@RequestBody HashMap map, HttpServletRequest req) {
        String url = zfMxxlServiceUrl + "/model/predict_many";
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("modelIds", map.get("modelIds"));
        paramsMap.put("predictNumber", map.get("predictNumber"));
        JSONObject object = HttpClientUtil.sendPost(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            return Result.OK(object.get("result"));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("模型预测失败");
            }
        }
    }

    @ApiOperation(value = "模型预测", notes = "模型预测")
    @PostMapping(value = "/model/predictPlus")
    public Result<Object> predictPlus(@RequestBody HashMap map, HttpServletRequest req) {
        String url = zfMxxlServiceUrl + "/model/predict_by_area";
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("slotIds", map.get("slotIds"));
        paramsMap.put("templateIds", map.get("templateIds"));
        paramsMap.put("predictNumber", map.get("predictNumber"));
        JSONObject object = HttpClientUtil.sendPost(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            return Result.OK(object.get("result"));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("模型预测失败");
            }
        }
    }

    @ApiOperation(value = "获取训练日志", notes = "获取训练日志-分页列表查询")
    @GetMapping(value = "/trainLog/list")
    public Result<IPage<Object>> queryTrainLogPageList(TrainLogParam trainLogParam, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        IPage<Object> pageList = new Page<>();
        String url = zfMxxlServiceUrl + "/model/get_train_log";
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("page", pageNo.toString());
        paramsMap.put("size", pageSize.toString());
        paramsMap.put("templateId", trainLogParam.getTemplateId());
        paramsMap.put("slotId", trainLogParam.getSlotId());
        JSONObject object = HttpClientUtil.sendGet(url, paramsMap);
        if (object.get("code") != null && Integer.parseInt(object.get("code").toString()) == 200) {
            JSONObject result = (JSONObject) object.get("data");
            pageList.setRecords((List<Object>) result.get("rows"));
            pageList.setTotal(Long.parseLong(result.get("total").toString()));
        } else {
            if (object.containsKey("message") && object.get("message") != null) {
                return Result.error(object.get("message").toString());
            } else {
                return Result.error("获取训练日志列表失败");
            }
        }

        return Result.OK(pageList);
    }
}
